package com.cloudweb.oa.expr;

import cn.js.fan.cache.jcs.RMCache;
import cn.js.fan.db.ListResult;
import cn.js.fan.util.ErrMsgException;
import cn.js.fan.util.StrUtil;
import com.cloudweb.oa.base.AbstractVisualFormDaoCache;
import com.cloudweb.oa.cache.BaseVisualFormDaoCache;
import com.cloudweb.oa.utils.ConstUtil;
import com.cloudwebsoft.framework.util.LogUtil;
import com.redmoon.oa.base.IFormDAO;
import com.redmoon.oa.flow.FormDb;
import com.redmoon.oa.visual.FormDAO;
import com.redmoon.oa.visual.FuncUtil;
import com.redmoon.oa.visual.func.FuncUnit;
import org.apache.commons.jcs3.access.exception.CacheException;
import org.jdom.Element;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Component
public class ExprCache extends BaseVisualFormDaoCache {

    public IFormDAO getExprDaoRaw(String code) {
        String sql = "select id from ft_" + ConstUtil.FORM_EXPRRESSION + " where code=" + StrUtil.sqlstr(code) + " order by id desc";
        FormDAO fdao = new FormDAO();
        try {
            ListResult listResult = fdao.listResult(ConstUtil.FORM_EXPRRESSION, sql, 1, 1);
            Iterator ir = listResult.getResult().iterator();
            if (ir.hasNext()) {
                return (FormDAO)ir.next();
            }
        } catch (ErrMsgException e) {
            LogUtil.getLog(getClass()).error(e);
        }
        return null;
    }

    public ExprUnit getExprUnit(String code) {
        try {
            ExprUnit funcUnit = (ExprUnit) RMCache.getInstance().getFromGroup(code, getGroup(ConstUtil.FORM_EXPRRESSION));
            if (funcUnit == null) {
                // 当formCode非法时，可能会被穿透，但考虑到本类仅用于FormDb更新时，故可能性不大
                IFormDAO formDAO = getExprDaoRaw(code);
                if (formDAO != null) {
                    String name = formDAO.getFieldValue("name");
                    String author = formDAO.getFieldValue("author");
                    String className = formDAO.getFieldValue("class");
                    String data = formDAO.getFieldValue("data");
                    String isDisplay = formDAO.getFieldValue("is_display");

                    // 1.2版后增加version，version=2表示为新版宏控件，启用description
                    float version = StrUtil.toFloat(formDAO.getFieldValue("version"), 1);

                    funcUnit = new ExprUnit(code);
                    funcUnit.setName(name);
                    funcUnit.setAuthor(author);
                    funcUnit.setClassName(className);
                    funcUnit.setData(data);
                    funcUnit.setDisplay(!"false".equals(isDisplay));
                    funcUnit.setVersion(version);

                    RMCache.getInstance().putInGroup(code, getGroup(ConstUtil.FORM_EXPRRESSION), funcUnit);
                    return funcUnit;
                } else {
                    LogUtil.getLog(getClass()).error("公式: " + code + " 不存在");
                    return null;
                }
            }
            else {
                return funcUnit;
            }
        } catch (CacheException e) {
            LogUtil.getLog(getClass()).error(e);
            return null;
        }
    }

    public void removeFromCache(String code) {
        try {
            RMCache.getInstance().remove(code, getGroup(ConstUtil.FORM_EXPRRESSION));
        } catch (CacheException e) {
            LogUtil.getLog(getClass()).error(e.getMessage());
        }
    }

    @Override
    public void refreshSave(IFormDAO iFormDAO) {
        removeFromCache(iFormDAO.getFieldValue("code"));
    }

    @Override
    public void refreshDel(IFormDAO iFormDAO) {
        removeFromCache(iFormDAO.getFieldValue("code"));
    }
}
